/**
 * @description 错误提示
 * @param {title} 提示文字
 * @c_time 2021-6-3
 */
export function errMsg(title, duration = 2000) {
	uni.showToast({
		title,
		icon: "none",
		duration: duration,
	});
}
/**
 * @author 骆敏乐
 * @description 唤起小程序
 * @c_time 2021-12-08
 */
export function arousalApplet(appletId) {
	// if (global.platform == 'android') {
	plus.share.getServices(function(res) {
			let sweixin = null;
			for (let i in res) {
				if (res[i].id == 'weixin') {
					sweixin = res[i];
				}
			}
			//唤醒微信小程序
			if (sweixin) {
				sweixin.launchMiniProgram({
					// 微信小程序的ID
					id: appletId, //原始ID
					type: 0, //微信小程序版本下
					path: 'pages/index/index' //跳转的小程序页面位置
				})
			}
		},
		function(e) {
			console.log('获取分享服务列表失败：' + e.message);
		})
	// } 
}
/**
 * @author 骆敏乐
 * @description 唤起客服
 * @c_time 2021-12-16
 */
export function openCustomerServiceChat() {
	// plus.runtime.openURL('https://work.weixin.qq.com/kfid/kfc948998bea39196d0');
	plus.share.getServices(function(res) {
			let sweixin = null;
			for (let i in res) {
				if (res[i].id == 'weixin') {
					sweixin = res[i];
				}
			}
			//唤醒微信小程序
			if (sweixin) {
				sweixin.openCustomerServiceChat({
					// 微信小程序的ID
					corpid: "wwfaa9d65d0575f162", //原始ID
					url: 'https://work.weixin.qq.com/kfid/kfc516cd85dd19dc416', //微信小程序版本下
				})
			}
		},
		function(e) {
			console.log('获取分享服务列表失败：' + e.message);
		})
}
/**
 * @author 骆敏乐
 * @method APP支付 - 赖良坤改写：加多平台参数判断
 * @param {type}
 */
export function requestPay(data) {
	return new Promise((resolve, reject) => {
		let obj = {
			provider: 'wxpay',
			orderInfo: data,
			success: function(peyRes) {
				resolve(peyRes);
			},
			fail: function(err) {
				reject(err);
			}
		}
		// #ifdef MP-TOUTIAO
		obj.service = 5
		// #endif
		uni.requestPayment(obj);
		// uni.requestPayment({
		// 	provider: 'wxpay',
		// 	orderInfo: data,
		// 	success: function(peyRes) {
		// 		resolve(peyRes);
		// 	},
		// 	fail: function(err) {
		// 		reject(err);
		// 	},
		// });
	});
}

export function isMobile(mobile) {
	if (!mobile) {
		// utils.toast('请输入手机号码')
		return false
	}
	if (!mobile.match(/^1[3-9][0-9]\d{8}$/)) {
		// utils.toast('手机号不正确')
		return false
	}
	return true
}
// 获取app 位置权限
export function openGps() {
	let system = uni.getSystemInfoSync(); // 获取系统信息
	if (system.platform === 'android') { // 判断平台
		var Intent = plus.android.importClass('android.content.Intent');
		var Settings = plus.android.importClass('android.provider.Settings');
		var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		var main = plus.android.runtimeMainActivity();
		main.startActivity(intent); // 打开系统设置GPS服务页面
	} else if (system.platform === 'ios') {
		var UIApplication = plus.ios.import("UIApplication");
		var application2 = UIApplication.sharedApplication();
		var NSURL2 = plus.ios.import("NSURL");
		var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
		application2.openURL(setting2);
		plus.ios.deleteObject(setting2);
		plus.ios.deleteObject(NSURL2);
		plus.ios.deleteObject(application2);
	}
}
export function throttle(fn, gapTime) {
	if (gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}

	let _lastTime = null

	// 返回新的函数
	return function() {
		let _nowTime = +new Date()
		if (_nowTime - _lastTime > gapTime || !_lastTime) {
			fn.apply(this, arguments) //将this和参数传给原函数
			_lastTime = _nowTime
		}
	}
}
export function trim(value) {
	if (!value) return '';
	let reg = /\s+/g;
	return value.replace(reg, '');
}